// const { dataTool } = require("echarts")

window.onload = async function () {
  try {
    const res = await axios({ url: '/dashboard', method: 'get' })
    console.log(res)
    const {
      data: {
        data: { overview },
      },
    } = res
    console.log(overview)

    // document.querySelector('span[name=salary').innerHTML=overview.salary;
    // document.querySelector('span[name=group_count').innerHTML =
    //   overview.group_count
    // document.querySelector('span[name=student_count').innerHTML =
    //   overview.student_count
    // document.querySelector('span[name=age').innerHTML=overview.age;

    for (let key in overview) {
      document.querySelector(`span[name=${key}]`).innerHTML = overview[key]
    }
    console.log(res.data.data.salaryData)
    line(res.data.data.year)
    pie(res.data.data.salaryData)
    bar(res.data.data.groupData)
    boyPie(res.data.data.salaryData)
    initMapChart(res.data.data.provinceData)
  } catch (error) {
    console.dir(error)
  }
}

function line(data) {
  //获取元素
  const chartDom = document.getElementById('line')
  //初始化图标
  const myChart = echarts.init(chartDom)
  //设置配置项
  myChart.setOption({
    // 设置标题
    title: {
      text: '2022全科薪资走势',
      top: '4%',
      left: '3%',
    }, // 设置X轴
    xAxis: {
      type: 'category',
      data: data.map((t) => t.month),
      axisLine: {
        lineStyle: {
          color: '#ccc',
          type: 'dashed',
        },
      },
    }, // 设置Y轴
    yAxis: {
      type: 'value', //分隔线的样式
      splitLine: {
        lineStyle: {
          type: 'dashed',
        },
      },
    },
    series: [
      {
        data: data.map((item) => item.salary),
        type: 'line',
        smooth: true,
        lineStyle: {
          width: 6,
        }, //标记的大小
        symbolSize: 10, //设置区域的渐变颜色
        areaStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: '#499FEE',
            },
            {
              offset: 0.8,
              color: 'rgba(255,255,255,0.2)',
            },
            {
              offset: 1,
              color: 'rgba(255,255,255,0)',
            },
          ]),
        },
      },
    ], //鼠标移动出现提示
    tooltip: {
      trigger: 'axis',
    }, //设置绘图网格
    grid: {
      //网格距离上方距离
      top: '20%',
    }, //设置折线图为渐变颜色
    color: [
      {
        type: 'linear',
        x: 0,
        y: 0,
        x2: 1,
        y2: 0,
        colorStops: [
          {
            offset: 0,
            color: '#499FEE', // 0% 处的颜色
          },
          {
            offset: 1,
            color: '#5D75F0', // 100% 处的颜色
          },
        ],
      },
    ],
  })
}

function pie(data) {
  const chartDom = document.getElementById('salary')
  const myChart = echarts.init(chartDom)

  myChart.setOption({
    //设置图表的标题和字体样式
    title: {
      text: '班级薪资分布',
      top: 15,
      left: 10,
      textStyle: {
        fontSize: 16,
      },
    },
    //图表的图例
    legend: {
      bottom: '6%',
      left: 'center',
    },
    color: ['#FDA224', '#5097FF', '#3ABCFA', '#34D39A'],
    tooltip: {
      trigger: 'item',
    },
    legend: {
      bottom: '6%',
      left: 'center',
    },
    series: [
      {
        name: '班级薪资分布',
        type: 'pie',
        radius: ['50%', '64%'],
        center: ['50%', '45%'],
        avoidLabelOverlap: false,
        itemStyle: {
          borderRadius: 10,
          borderColor: '#fff',
          borderWidth: 2,
        },
        label: {
          show: false,
          position: 'center',
        },
        emphasis: {
          label: {
            show: false,
            fontSize: '40',
            fontWeight: 'bold',
          },
        },
        labelLine: {
          show: false,
        },
        data: data.map((item) => {
          return {
            value: item.g_count + item.b_count,
            name: item.label,
          }
        }),
      },
    ],
  })
}

function bar(data) {
  const chartDom = document.getElementById('lines')
  const myChart = echarts.init(chartDom)
  let option

  option = {
    grid: {
      left: 70,
      top: 30,
      right: 30,
      bottom: 50,
    },
    xAxis: {
      type: 'category',
      data: data[1].map((item) => item.name),
      axisLine: {
        lineStyle: {
          color: '#ccc',
          type: 'dashed',
        },
      },
      axisLabel: {
        color: '#999',
      },
    },
    yAxis: {
      type: 'value',
      splitLine: {
        lineStyle: {
          type: 'dashed',
        },
      },
    },
    tooltip: {
      trigger: 'item',
    },
    color: [
      {
        type: 'linear',
        x: 0,
        y: 0,
        x2: 0,
        y2: 1,
        colorStops: [
          {
            offset: 0,
            color: '#34D39A', // 0% 处的颜色
          },
          {
            offset: 1,
            color: 'rgba(52,211,154,0.2)', // 100% 处的颜色
          },
        ],
      },
      {
        type: 'linear',
        x: 0,
        y: 0,
        x2: 0,
        y2: 1,
        colorStops: [
          {
            offset: 0,
            color: '#499FEE', // 0% 处的颜色
          },
          {
            offset: 1,
            color: 'rgba(73,159,238,0.2)', // 100% 处的颜色
          },
        ],
      },
    ],
    series: [
      {
        // data: [12200, 17932, 13901, 13934, 21290, 23300, 13300, 13320],
        data: data[1].map((item) => item.hope_salary),
        type: 'bar',
        name: '期望薪资',
      },
      {
        // data: [22820, 19932, 16901, 15934, 31290, 13300, 14300, 18320],
        data: data[1].map((item) => item.salary),
        type: 'bar',
        name: '就业薪资',
      },
    ],
  }

  option && myChart.setOption(option)

  const btns = document.querySelector('#btns')
  btns.addEventListener('click', function (e) {
    if (e.target.tagName === 'BUTTON') {
      console.log(e.target.innerHTML)
      //排他思想
      btns.querySelector('.btn-blue').classList.remove('btn-blue')
      e.target.classList.add('btn-blue')

      let group = e.target.innerHTML
      console.log(data[group]);
      option.xAxis.data = data[group].map((item) => item.name)
      option.series[0].data = data[group].map((item) => item.hope_salary)
      option.series[1].data = data[group].map((item) => item.salary)
      //生成图表
      myChart.setOption(option)
    }
  })
}

function boyPie(data) {
  const chartDom = document.getElementById('gender')
  const myChart = echarts.init(chartDom)

  myChart.setOption({
    //设置图表的标题和字体样式
    title: [
      {
        text: '男女薪资分布',
        top: '3%',
        left: '3%',
      },
      {
        text: '男生',
        top: '48%',
        left: 'center',
        textStyle: {
          color: '#333',
          fontSize: 14,
          fontWeight: 'bold',
        },
      },
      {
        text: '女生',
        top: '93%',
        left: 'center',
        textStyle: {
          color: '#333',
          fontSize: 14,
          fontWeight: 'bold',
        },
      },
    ],
    tooltip: {
      trigger: 'item',
    },
    legend: {
      show: false,
    },
    color: ['#FDA224', '#5097FF', '#3ABCFA', '#34D39A'],
    series: [
      {
        name: '女生薪资',
        type: 'pie',
        center: ['50%', '75%'],
        radius: ['40%', '25%'],
        avoidLabelOverlap: false,
        label: {
          show: true,
          position: 'outside',
        },
        // emphasis: {
        //   label: {
        //     show: 'true',
        //     position: 'outside',
        //     fontSize: '40',
        //     fontWeight: 'bold',
        //   },
        // },
        labelLine: {
          show: true,
          showAbove: true,
          length: 50,
        },
        data: data.map((item) => {
          return {
            value: item.g_count + item.b_count - 2,
            name: item.label,
          }
        }),
      },
      {
        name: '男生薪资',
        type: 'pie',
        center: ['50%', '30%'],
        radius: ['40%', '25%'],
        avoidLabelOverlap: false,
        label: {
          show: true,
          position: 'outside',
        },
        emphasis: {
          label: {
            show: 'true',
            position: 'outside',
            fontSize: '40',
            fontWeight: 'bold',
          },
        },
        labelLine: {
          show: true,
          showAbove: true,
          length: 50,
        },
        data: data.map((item) => {
          return {
            value: item.g_count + item.b_count,
            name: item.label,
          }
        }),
      },
    ],
  })
}

const initMapChart = (provinceData) => {
  const myechart = echarts.init(document.querySelector('#map'))
  provinceData = JSON.parse(
     JSON.stringify(provinceData).replace(/省|自治区|维吾尔|回族|壮族/g, '')
  )
  
  console.log(provinceData);
  let option = {
    title: {
      text: '籍贯分布',
      top: 10,
      left: 10,
      textStyle: {
        fontSize: 16,
      },
    },
    tooltip: {
      trigger: 'item',
      formatter: '{b}: {c} 位学员',
      borderColor: 'transparent',
      backgroundColor: 'rgba(0,0,0,0.5)',
      textStyle: {
        color: '#fff',
      },
    },
    visualMap: {
      min: 0,
      max: 6,
      left: 'left',
      bottom: '20',
      text: ['6', '0'],
      inRange: {
        color: ['#ffffff', '#0075F0'],
      },
      show: true,
      left: 40,
    },
    geo: {
      map: 'china',
      roam: false,
      zoom: 1.0,
      label: {
        normal: {
          show: true,
          fontSize: '10',
          color: 'rgba(0,0,0,0.7)',
        },
      },
      itemStyle: {
        normal: {
          borderColor: 'rgba(0, 0, 0, 0.2)',
          color: '#e0ffff',
        },
        emphasis: {
          areaColor: '#34D39A',
          shadowOffsetX: 0,
          shadowOffsetY: 0,
          shadowBlur: 20,
          borderWidth: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)',
        },
      },
    },
    series: [
      {
        name: '籍贯分布',
        type: 'map',
        geoIndex: 0,
        data: provinceData,
      },
    ],
  }
  myechart.setOption(option)
}
